package base

import (
	"gitee.com/gopher2011/go-zero-gva/internal/svc"
	"gitee.com/gopher2011/go-zero-gva/internal/types"
	"gitee.com/gopher2011/go-zero-gva/utils"
	"github.com/mojocn/base64Captcha"
)

// 当开启多服务器部署时，替换下面的配置，使用redis共享存储验证码
//var store = NewCaptcha
var store = base64Captcha.DefaultMemStore

//func (l *Base) Login(req types.LoginReq) (*types.Response, error) {
//	var user svc.SysUser
//	if store.Verify(req.CaptchaId,req.Captcha,true) {
//		password := utils.MD5V([]byte(req.Password))
//		err := l.SvcCtx.DB.Where("username = ? and password = ?",req.Username,password).Preload("Authority").Preload("Authorities").First(&user).Error
//		if err != nil {
//			return &types.Response{Code: 7,Msg: "用户名不存在或者密码错误"}, nil
//		}
//		return l.getJwtToken(user)
//	}else {
//		return &types.Response{Code: 7,Msg: "验证码错误"}, nil
//	}
//}

func (l *Base) Login(req types.LoginReq) (*types.Response, error) {
	var user svc.SysUser
	password := utils.MD5V([]byte(req.Password))
	l.SvcCtx.DB.Where(user.Username)
	err := l.SvcCtx.DB.Where("username = ? and password = ?", req.Username, password).Preload("Authority").Preload("Authorities").First(&user).Error
	if err != nil {
		return &types.Response{Code: 7, Msg: "用户名不存在或者密码错误"}, nil
	}
	//return &types.Response{Code: 0,Msg: "登录成功",Data: user}, nil
	return &types.Response{Code: 7, Msg: "用户名不存在或者密码错误"}, nil

}

func (l *Base) getJwtToken(secretKey string, iat, seconds, userId int64) (string, error) {
	claims := make(jwt.MapClaims)
	claims["expire"] = iat + seconds
	claims["iat"] = iat
	claims["userId"] = userId
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(secretKey))
}

/*接口测试
Post /base/login
{
   "username":"admin01",
   "password":"root001",
   "captcha":"afaf",
	"captchaId":"fafaf"
}
*/
/*
{
	"code": 0,
	"data": {
		"user": {
			"ID": 1,
			"CreatedAt": "2021-10-11T19:02:54.808+08:00",
			"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
			"uuid": "c632ef42-e03a-4046-a9f2-b68fbe189ca5",
			"userName": "admin01",
			"nickName": "vip",
			"headerImg": "https://",
			"authority": {
				"CreatedAt": "0001-01-01T00:00:00Z",
				"UpdatedAt": "0001-01-01T00:00:00Z",
				"DeletedAt": null,
				"authorityId": "",
				"authorityName": "",
				"parentId": "",
				"dataAuthorityId": null,
				"children": null,
				"menus": null,
				"defaultRouter": ""
			},
			"authorityId": "999",
			"sideMode": "dark",
			"activeColor": "#1890ff",
			"baseColor": "#fff",
			"authorities": [
				{
					"CreatedAt": "2021-10-11T19:02:54.808+08:00",
					"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
					"DeletedAt": null,
					"authorityId": "001",
					"authorityName": "",
					"parentId": "",
					"dataAuthorityId": null,
					"children": null,
					"menus": null,
					"defaultRouter": "dashboard"
				},
				{
					"CreatedAt": "2021-10-11T19:02:54.808+08:00",
					"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
					"DeletedAt": null,
					"authorityId": "002",
					"authorityName": "",
					"parentId": "",
					"dataAuthorityId": null,
					"children": null,
					"menus": null,
					"defaultRouter": "dashboard"
				},
				{
					"CreatedAt": "2021-10-11T19:02:54.808+08:00",
					"UpdatedAt": "2021-10-11T19:02:54.808+08:00",
					"DeletedAt": null,
					"authorityId": "003",
					"authorityName": "",
					"parentId": "",
					"dataAuthorityId": null,
					"children": null,
					"menus": null,
					"defaultRouter": "dashboard"
				}
			]
		},
		"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVVUlEIjoiYzYzMmVmNDItZTAzYS00MDQ2LWE5ZjItYjY4ZmJlMTg5Y2E1IiwiSUQiOjEsIlVzZXJuYW1lIjoiYWRtaW4wMSIsIk5pY2tOYW1lIjoidmlwIiwiQXV0aG9yaXR5SWQiOiI5OTkiLCJCdWZmZXJUaW1lIjo4NjQwMCwiZXhwIjoxNjM0NTU2MDI3LCJpc3MiOiJxbVBsdXMiLCJuYmYiOjE2MzM5NTAyMjd9.AT0fd_HdrlUPmT_ySGST9Fp5r6gywax-ESTXA4ijX8A",
		"expiresAt": 1634556027000
	},
	"msg": "登录成功"
}
*/
